<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="${mapperPackageName}.${table.className}Mapper">
<#assign escapeStr="#" >
<#macro keySql>
    <#list table.ids as id>
        <#assign conStr="" >
        <#if id_index gt 1>
            <#assign conStr=" and " >
        </#if>
    ${conStr}${id.name} = ${escapeStr}{${id.fieldName},jdbcType=${id.typeName ? upper_case}}
    </#list>
</#macro>
<#macro conSql>
    <#if table.columns??>
        <#list table.columns as item>
            <if test="${item.fieldName} != null and ${item.fieldName} != ''">
                and ${item.name} = ${escapeStr}{${item.fieldName},jdbcType=${item.typeName ? upper_case}}
            </if>
        </#list>
    </#if>
</#macro>

    <resultMap id="BaseResultMap" type="${modelPackageName}.${table.className}">
    <#list table.ids as id>
        <id column="${id.name}" property="${id.fieldName}" jdbcType="${id.typeName ? upper_case}"/>
    </#list>
    <#if table.columns??>
        <#assign colStr="">
        <#list table.columns as item>
            <#assign colStr="${colStr},${item.name}">
            <#assign isBool=true>
            <#list table.ids as id>
                <#if id.fieldName == item.fieldName >
                    <#assign isBool=false>
                </#if>
            </#list>
            <#if isBool>
                <result column="${item.name}" property="${item.fieldName}" jdbcType="${item.typeName ? upper_case}"/>
            </#if>
        </#list>
    </#if>
    <#assign colStr="${colStr ? substring(1,(colStr ? length))}">
    </resultMap>

    <sql id="Base_Column_List">
    ${colStr}
    </sql>

    <sql id="where_sql">
        <where>
            ${conSql}
        </where>
    </sql>

<#assign keyType="" >
<#if table.ids?size gt 1 >
    <#assign keyType="${table.className!}Key" >
</#if>
<#if table.ids?size == 1 >
    <#switch table.ids[0].fieldType>
        <#case "String">
            <#assign keyType="java.lang.String" >
            <#break>
        <#case "Date">
            <#assign keyType="java.util.Date" >
            <#break>
        <#default>
    </#switch>
</#if>

    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="${keyType}">
        select
        <include refid="Base_Column_List"/>
        from ${table.name}
        where
    <@keySql />
    </select>

    <select id="select" resultMap="BaseResultMap" parameterType="${modelPackageName}.${table.className}">
        select
        <include refid="Base_Column_List"/>
        from ${table.name}
        <include refid="where_sql"/>
    </select>

    <delete id="deleteByPrimaryKey" parameterType="${keyType}">
        delete from ${table.name}
        where
    <@keySql />
    </delete>

    <delete id="delete" parameterType="${modelPackageName}.${table.className}">
        delete from ${table.name}
        <include refid="where_sql"/>
    </delete>

    <insert id="insert" parameterType="${modelPackageName}.${table.className}">
        insert into ${table.name}
        <trim prefix="(" suffix=")" suffixOverrides=",">
        <#if table.columns??>
            <#list table.columns as item>
                <if test="${item.fieldName} != null and ${item.fieldName} != ''">
                ${item.name},
                </if>
            </#list>
        </#if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
        <#if table.columns??>
            <#list table.columns as item>
                <if test="${item.fieldName} != null and ${item.fieldName} != ''">
                ${escapeStr}{${item.fieldName},jdbcType=${item.typeName ? upper_case}},
                </if>
            </#list>
        </#if>
        </trim>
    </insert>

    <update id="update" parameterType="${modelPackageName}.${table.className}">
        update ${table.name}
        <set>
        <#list table.columns as item>
            <if test="${item.fieldName} != null and ${item.fieldName} != ''">
            ${item.name} = ${escapeStr}{${item.fieldName},jdbcType=${item.typeName ? upper_case}},
            </if>
        </#list>
        </set>
        where
    <@keySql />
    </update>
</mapper>